﻿angular.module('Caltech').controller('workOrderController', function ($scope, Restangular, ngTableParams, $stateParams, $location, $filter, ProcessStatusWO) {
    'use strict';

    $scope.customer = { name: '' };
    $scope.filters = {};

    $scope.paging = {
        index: 1,
        size: 10
    };

    $scope.filter = false;

    $scope.getCustomerList = function (nameFitler) {
        return Restangular.all('customer/list').getList({
            name: nameFitler
        });
    };

    $scope.search = function (customer, index, salesman) {
        if (typeof (customer) === 'string') {
            $scope.customer = {
                name: customer
            }
        } else if (typeof (customer) === 'object') {
            $scope.customer = customer;
        }

        if (salesman) {
            $scope.filters.salesman = salesman;
        }

        if ($scope.tableParams) {
            $scope.tableParams.reload();
        } else {
            $scope.tableParams = new ngTableParams({
                page: $scope.paging.index,
                count: $scope.paging.size
            }, {
                counts: [15, 20, 25],
                getData: function ($defer, params) {
                    var customerId = $scope.customer.id || undefined,
                        customerName = customerId === undefined && $scope.customer.name ? $scope.customer.name : undefined;
                    if ($scope.filter) {
                        $scope.filter = false;
                        params.page(1);
                    }

                    Restangular.all('workOrder')
                        .getList({
                            page: params.page(),
                            count: params.count(),
                            customerName: customerName,
                            customerId: customerId,
                            salesman: $scope.filters.salesman
                        }).then(function (data) {
                            params.total(data.total);
                            $defer.resolve(data);

                            if (params.page() != $scope.paging.index) {
                                $location.search('pageIndex', params.page());
                                $scope.paging.index = params.page();
                            }

                            if (params.count() != $scope.paging.size) {
                                $location.search('pageIndex', 1);
                                $location.search('pageSize', params.count());
                                $scope.paging.size = params.count();
                            }

                            $location.search('customerName', $scope.customer.name || undefined);
                            $location.search('customerId', $scope.customer.id || undefined);
                            $location.search('salesman', $scope.filters.salesman || undefined);
                        });
                }
            });
        }
    };

    $scope.updateCustomer = function (item) {
        $location.search('pageIndex', 1);
        $scope.paging.index = 1;
        $scope.filter = true;
        $scope.search({ id: item.id, name: item.name });
    };

    $scope.reSearch = function () {
        $location.search('pageIndex', 1);
        $scope.paging.index = 1;
        $scope.filter = true;
        $scope.search();
    };

    $scope.status = ProcessStatusWO.map(function (value) {
        var ordinal = $filter('ordinal');
        value.text = ordinal(value.text);
        return value;
    });

    $scope.showStatus = function (value) {
        var result = '';
        if (value) {
            var selected = $filter('filter')($scope.status, { value: value });
            if (selected.length) {
                result = selected[0].text;
            }
        }
        return result;
    };

    if ($stateParams.customerId || $stateParams.customerName || $stateParams.pageIndex || $stateParams.pageSize || $stateParams.salesman) {

        $scope.paging = {
            index: $stateParams.pageIndex || 1,
            size: $stateParams.pageSize || 10
        };

        $scope.filters.salesman = $stateParams.salesman || undefined;

        $scope.search(
            {
                id: $stateParams.customerId,
                name: $stateParams.customerName
            }, $stateParams.salesman);
    } else {
        $scope.search();
    }

});